package com.hk.yajin.function.news;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hk.developmentlib.baseben.NewWorkBean;
import com.hk.developmentlib.callback.HttpCallBack;
import com.hk.developmentlib.utills.FastjsonUtils;
import com.hk.developmentlib.utills.UiUtil;
import com.hk.yajin.R;
import com.hk.yajin.base.SingleItemListFragment;
import com.hk.yajin.manager.UserInfoManager;
import com.hk.yajin.model.DataListModel;
import com.hk.yajin.model.LunBoData;
import com.hk.yajin.model.News;
import com.hk.yajin.utills.HttpRequestUtils;
import com.hk.yajin.utills.url.UrlConstants;
import com.jude.rollviewpager.OnItemClickListener;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;

import org.xutils.common.util.LogUtil;

import java.util.List;
import java.util.Map;

/**
 * Created by 伦哥哥 on 2017/6/25.
 */

public class MenuNewsFragment extends SingleItemListFragment<News> {
    private Toolbar toolbar;
    private RollPagerView rollPagerView;
    private NewsHeadViewAdapter headViewAdapter;


    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        mContentView = inflater.inflate(R.layout.fragment_menu_news, container, false);
        initToolBar();
        initSwipeView();
        initAdapter();
        initRecyclerView();
        // initHeadView();
        return mContentView;
    }

    private void initToolBar() {
        toolbar = (Toolbar) mContentView.findViewById(R.id.toolbar);
        toolbar.setBackgroundColor(UiUtil.getColor(R.color.white));
        toolbar.setTitle("");
        TextView textView = (TextView) mContentView.findViewById(R.id.tv_title);
        textView.setText(UiUtil.getString(R.string.title_news));
        textView.setTextColor(UiUtil.getColor(R.color.black_282828));
    }

    @Override
    public void initAdapter() {
        mQuickAdapter = new NewsListQuickAdapter(dataList, getActivity());
        mQuickAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                Intent intent = new Intent(getActivity(), ArticleDetailActivity.class);
                intent.putExtra(DATA_STRING, FastjsonUtils.toJSONString(dataList.get(position)));
                startActivity(intent);
            }
        });
        super.initAdapter();
    }

    @Override
    public void setItemDecoration() {
        // mRecyclerView.addItemDecoration(new BaseItemBottomDecoration(getActivity(), 1));
    }

    private void initHeadView() {
        headview = getActivity().getLayoutInflater().inflate(R.layout.list_head_news, null, false);
        rollPagerView = (RollPagerView) headview.findViewById(R.id.pv_lunbo);
        rollPagerView.setHintView(new ColorPointHintView(getActivity(), Color.YELLOW, Color.WHITE));
        rollPagerView.getLayoutParams().height = UiUtil.getScreenWidth() * 12 / 25;
        headViewAdapter = new NewsHeadViewAdapter(headviewDatas, getActivity(), rollPagerView);
        rollPagerView.setAdapter(headViewAdapter);
        rollPagerView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                showShortToast("position");
            }
        });
        mQuickAdapter.addHeaderView(headview);
        headViewAdapter.notifyDataSetChanged();

    }

    @Override
    public void initDataList() {
        if (loadFinish) {
            loadFinish = false;
            if (page == 1) {
                showLoadingView();
            }
            Map<String, Object> reqParam = UserInfoManager.getUserKeyMapIfLogin();
            reqParam.put("per_page", String.valueOf(pageSize));
            reqParam.put("page", String.valueOf(page));

            HttpRequestUtils.post(true, UrlConstants.FUNCTION_VIEW_NEWS_LIST, reqParam, false, new HttpCallBack() {
                @Override
                public void onSucced(String data) {
                    hideLoadingView();
                    if (swipe_container != null && swipe_container.isRefreshing()) {
                        dataList.clear();
                    }
                    swipe_container.setRefreshing(false);
                    LogUtil.d("成功：" + data);
                    if (!TextUtils.isEmpty(data)) {
                        DataListModel<News> model = FastjsonUtils.getBeanObject(data, new TypeReference<DataListModel<News>>() {
                        });
                        handleData(model.getData());

                    } else {
                        mQuickAdapter.loadMoreFail();
                        mQuickAdapter.notifyDataSetChanged();
                        loadFinish = true;
                    }
                }

                @Override
                public void onBadHttp(String tip) {
                    hideLoadingView();
                    swipe_container.setRefreshing(false);

                    showShortToast(tip);
                }

                @Override
                public void onErrorCode(NewWorkBean bean) {
                    hideLoadingView();
                    swipe_container.setRefreshing(false);

                    showShortToast(bean.getMsg());
                }
            });
        }
    }


    @Override
    protected void initLunBo() {
//        Map<String, Object> reqParam = UserInfoManager.getUserKeyMapIfLogin();
//        reqParam.put("cl_id", String.valueOf("1"));
//        reqParam.put("limit", String.valueOf("3"));
//
//        HttpRequestUtils.post(false, UrlConstants.FUNCTION_LUN_BO, reqParam, false, new HttpCallBack() {
//            @Override
//            public void onSucced(String data) {
//                List<LunBoData> headDatas = FastjsonUtils.getBeanList(data, LunBoData.class);
//                if (headDatas != null && headDatas.size() > 0) {
//                    headviewDatas.clear();
//                    headviewDatas.addAll(headDatas);
//                    headViewAdapter.notifyDataSetChanged();
//                    headview.setVisibility(View.VISIBLE);
//                } else {
//                    headview.setVisibility(View.GONE);
//                }
//            }
//
//            @Override
//            public void onBadHttp(String tip) {
//
//            }
//
//            @Override
//            public void onErrorCode(NewWorkBean bean) {
//
//            }
//        });
    }


}
